package com.cloudsigns.factorymanager2.produce.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.customview.ListViewForScrollView;
import com.cloudsigns.factorymanager2.produce.adapter.ProduceProcessInforPlanAdapter;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartBean;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;


public class ProduceWorkpartInformationActivity extends BaseActivity {
    private String TAG = ProduceWorkpartInformationActivity.class.getSimpleName();
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_process_name;
    private ImageView img_process_down;
    private LinearLayout ll_content;
    private TextView edt_specifition_size, edt_slip_size, edt_process_num, edt_start_date, edt_completed_date;
    private ListViewForScrollView lv_cycle_num, lv_rework_cycle;
    private TextView btn_save, btn_delete;
    private WorkPartBean workPartBean;

    private ProduceProcessInforPlanAdapter planAdapter;

    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_produce_workpart_information);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        tv_process_name = findViewById(R.id.tv_process_name);
        img_process_down = findViewById(R.id.img_process_down);
        ll_content = findViewById(R.id.ll_content);
        edt_specifition_size = findViewById(R.id.edt_specifition_size);
        edt_slip_size = findViewById(R.id.edt_slip_size);
        edt_process_num = findViewById(R.id.edt_process_num);
        edt_start_date = findViewById(R.id.edt_start_date);
        edt_completed_date = findViewById(R.id.edt_completed_date);
        lv_cycle_num = findViewById(R.id.lv_cycle_num);
        lv_rework_cycle = findViewById(R.id.lv_rework_cycle);
        btn_save = findViewById(R.id.btn_save);
        btn_delete = findViewById(R.id.btn_delete);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tv_title.setText(getResources().getString(R.string.process_information));
        img_process_down.setBackgroundResource(R.mipmap.arrow_down_icon);

        Intent intent = getIntent();
        if (null != intent) {
            workPartBean = (WorkPartBean) intent.getSerializableExtra(ConstantUtil.KEY_1);
        }

        planAdapter = new ProduceProcessInforPlanAdapter(this);
        planAdapter.setData(workPartBean.getWorkPartScheduleList());
        lv_cycle_num.setAdapter(planAdapter);

        CommonEnum.ProductShapeType shape = workPartBean.getShapeType().getType();
        if (null != shape) {
            if (shape == CommonEnum.ProductShapeType.ROUND) {
                edt_specifition_size.setText("直径" + workPartBean.getShapeType().getDiameter() + "mm*" + "厚度" + workPartBean.getShapeType().getThick() + "mm");
            } else if (shape == CommonEnum.ProductShapeType.RECTANGLE) {
                edt_specifition_size.setText("长度" + workPartBean.getShapeType().getWidth() + "mm*" + "高度" + workPartBean.getShapeType().getHeight() + "mm*" + "厚度" + workPartBean.getShapeType().getThick() + "mm");
            } else if (shape == CommonEnum.ProductShapeType.SPECIALSHAPE) {
                edt_specifition_size.setText(workPartBean.getShapeType().getDesc());
            }
        }

        edt_slip_size.setText("上" + workPartBean.getShapeType().getBorderTop() + "mm*" + "下" + workPartBean.getShapeType().getBorderBottom() + "mm*" + "左" + workPartBean.getShapeType().getBorderLeft() + "mm*" + "右" + workPartBean.getShapeType().getBorderRight() + "mm");
        tv_process_name.setText(workPartBean.getWorkPartName());
        edt_process_num.setText(workPartBean.getAmount() + workPartBean.getUnit());
        edt_start_date.setText(DateUtil.getYyyy_Mm_Dd(workPartBean.getStartDate()));
        edt_completed_date.setText(DateUtil.getYyyy_Mm_Dd(workPartBean.getCompleteDate()));

    }

    @Override
    public void setListener() {
        rl_back.setOnClickListener(this);
        img_process_down.setOnClickListener(this);
        btn_save.setOnClickListener(this);
        btn_delete.setOnClickListener(this);
    }

    // 工序名称 收放
    private void showProcessContent() {
        if (ll_content.getVisibility() == View.VISIBLE) {
            ll_content.setVisibility(View.GONE);
            img_process_down.setBackgroundResource(R.mipmap.arrow_down_icon);
        } else {
            ll_content.setVisibility(View.VISIBLE);
            img_process_down.setBackgroundResource(R.mipmap.arrow_top);
        }
    }

    @Override
    public void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            // 工序名称 收放
            case R.id.img_process_down:
                showProcessContent();
                break;
            // 保存
            case R.id.btn_save:
                if (workPartBean.getScheduleAmountAll() <= workPartBean.getAmount()) {
                    if (null != workPartBean.getWorkPartScheduleList() || !workPartBean.getWorkPartScheduleList().isEmpty()) {
                        Intent mIntent = new Intent();
                        mIntent.putExtra(ConstantUtil.WORKPARTBEAN, workPartBean);
                        setResult(ConstantUtil.DELETE_WORKPART_INFOMATION, mIntent);
                        showToast(getResources().getString(R.string.save_success));
                        finish();
                    }
                } else {
                    showToast(getResources().getString(R.string.plan_no_greater_process));
                }
                break;
            // 删除
            case R.id.btn_delete:
                if (!TextUtils.isEmpty(workPartBean.getWorkPartId())) {
                    //todo 调接口
                } else {
                    workPartBean.resetDefault();
                    Intent mIntent = new Intent();
                    mIntent.putExtra(ConstantUtil.WORKPARTBEAN, workPartBean);
                    setResult(ConstantUtil.DELETE_WORKPART_INFOMATION, mIntent);
                    showToast(getResources().getString(R.string.delete_success));
                    finish();
                }
                break;
        }
    }
}
